package com.distributor;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
 

public class TabMain extends TabActivity implements OnTabChangeListener
{
    /** Called when the activity is first created. */
	
	TabHost tabHost;
	TabHost.TabSpec spec;
 	Intent intent; 
    Resources res;
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//		WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
        setContentView(R.layout.tablayout);
        
        res = getResources(); 
        tabHost = getTabHost();
        
        intent = new Intent().setClass(this,ActivityMenu.class);
        spec = tabHost.newTabSpec("Home").setIndicator("")
                .setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        tabHost.addTab(spec);
        
        
        intent = new Intent().setClass(this,Favourite.class);
        spec = tabHost.newTabSpec("Favourite").setIndicator("")
                .setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        tabHost.addTab(spec);
        
        
        intent = new Intent().setClass(this,ComingSoon.class);
        spec = tabHost.newTabSpec("Balance").setIndicator("")
                       .setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        tabHost.addTab(spec);
        tabHost.setCurrentTab(0);
        
        tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.home_active);
        tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.favourite);
        tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.balance);
        tabHost.setOnTabChangedListener(this);

    }
	public void onTabChanged(String tabName) 
	{
		if(tabName.equalsIgnoreCase("Home"))
		{
			tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.home_active);
			tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.favourite);
		    tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.balance);
		}
		else if(tabName.equalsIgnoreCase("Favourite"))
		{
			tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.favourite_active);
			tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.home);
		    tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.balance);
		}
		else if(tabName.equalsIgnoreCase("Balance"))
		{
			tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.balance_active);
			tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.home);
		    tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.favourite);
		}
				
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
	            //Code Comes here.....
       	System.out.println("Key Event:"+event.getAction()+",keyCode"+keyCode);
        onBackPressed();
        return true;
        	
	}
	 
	public void onBackPressed() 
	{
		// TODO Auto-generated method stub
		
		 new AlertDialog.Builder(TabMain.this)
        .setTitle( "Exit Application" )
        .setMessage( "Are you sure you want to Exit" )
        .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface arg0, int arg1) 
            {
                //do stuff onclick of YES
            	finish();
            }

			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				
			}
        })
        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener() 
         {
          	public void onClick(DialogInterface arg0, int arg1)
            {
            	//do stuff onclick of CANCEL
            	arg0.dismiss();
            }
            
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
			}
			
        }).show();
	}
}
    
